// @ts-check
import * as THREE from 'three'
import * as CANNON from 'cannon'

class Bullet {
  constructor({
    position,
    rotation
  }) {
    let { x, y, z } = position
    const { x: viewX, y: viewY } = rotation
    this.speed = 100
    const material = new THREE.MeshBasicMaterial({ color: 'orange' })
    const ballBody = new CANNON.Body({ mass: 1 })
    const ballShape = new CANNON.Sphere(0.2)
    const ballGeometry = new THREE.SphereGeometry(ballShape.radius, 32, 32)
    ballBody.addShape(ballShape)
    const ballMesh = new THREE.Mesh(ballGeometry, material)

    ballMesh.castShadow = true
    ballMesh.receiveShadow = true

    const shootV = {
      x: -this.speed * Math.sin(viewY),
      y: this.speed * Math.sin(viewX),
      z: -this.speed * Math.cos(viewY)
    }


    ballBody.velocity.set(shootV.x, shootV.y, shootV.z)
    ballBody.position.set(x, y, z)

    this._body = ballBody
    this._mesh = ballMesh
  }

  get object() {
    return this._mesh
  }

  get cannonBody() {
    return this._body
  }

  next = () => {
    this._mesh.position.copy(this._body.position)
    this._mesh.quaternion.copy(this._body.quaternion)
  }
}

export default Bullet
